package com.iamteer.contact.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.contact.dao.impl.ContactDaoImpl;
import com.iamteer.contact.entity.Contact;

/**
 * Servlet implementation class ListContact
 */
@WebServlet("/ListContact")
public class ListContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ListContact() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1. 从 xml 中读取出联系人数据
		ContactDaoImpl service = new ContactDaoImpl();
		List<Contact> list = service.findAll();
		
		//2. 把结果保存到域对象中
		request.setAttribute("contacts", list);
		
		//3. 跳转到 jsp 页面
		request.getRequestDispatcher("/listContact.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
